//
//  QXBarButtonItem.swift
//  QXWeibo
//
//  Created by QinXin on 15/6/29.
//  Copyright © 2015年 labi3285_Lab. All rights reserved.
//

import UIKit

class QXBarButtonItem: UIBarButtonItem {
    
    var btnClickedDo: (() -> ())?
    
    // 快速创建
    convenience init(imageName: String, highlightedImageName: String?, clicked: (() -> ())?) {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal);
        // ?? 表示如果前面的内容为 nil，使用后面的内容
        let hImageName = highlightedImageName ?? imageName + "_highlighted"
        
        btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        self.init(customView: btn)
        
        btn.addTarget(self, action: "btnClicked", forControlEvents: UIControlEvents.TouchUpInside)
        btnClickedDo = clicked
        
    }
    
    func btnClicked() {
        btnClickedDo?()
    }
    
    convenience init(imageName: String, highlightedImageName: String?, target: AnyObject?, actionName: String?) {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal);
        
        let hImageName = highlightedImageName ?? imageName + "_highlighted"
        btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        // 添加监听方法
        if actionName != nil {
            btn.addTarget(target, action: Selector(actionName!), forControlEvents: UIControlEvents.TouchUpInside)
        }
        
        self.init(customView: btn)
    }
}
